package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.aspectj.login.LoginCheck;
import com.sgb.kjwl.databinding.AtFindPeopleDetailBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.self.SelfWorkerPublishEntity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.sgb.kjwl.viewmodel.packers.FindPeopleDetailModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.SharedPreferenceUtil;
import com.tencent.smtt.sdk.WebSettings;

import javax.inject.Inject;

/**
 * @author liwei
 * @date 2021/3/26
 * description:找工人
 */
public class FindPeopleDetailActivity extends AppActivity {

    @Inject
    FindPeopleDetailModel viewModel;
    private AtFindPeopleDetailBinding binding;
    private boolean isAttention = false;

    public static void start(Context context, String no) {
        Intent intent = new Intent();
        intent.setClass(context, FindPeopleDetailActivity.class);
        intent.putExtra("no", no);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_find_people_detail);
        binding.setViewModel(viewModel);
        binding.linAttention.setOnClickListener(this);
        binding.linTellPhone.setOnClickListener(this);
        binding.linDetailTool.setOnClickListener(this);
    }

    @Override
    protected void init() {
        viewModel.getWorkerDetail(getIntent().getStringExtra("no"));
        viewModel.getH5Url(getIntent().getStringExtra("no"));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.lin_attention) {
            followCheck();
        } else if (id == R.id.lin_tell_phone) {
            detailPhone();
        }
    }

    @LoginCheck(needRealName = true)
    public void followCheck(){
        viewModel.follow(isAttention,getIntent().getStringExtra("no"));
    }

    @LoginCheck
    public void detailPhone(){
        viewModel.detailTeamTelephone(getIntent().getStringExtra("no"));
    }

    public void getUrl(String url) {
        binding.progress.show();
        binding.progress.setColor(ContextCompat.getColor(this,R.color.translucent));
        binding.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        binding.webView.getSettings().setDomStorageEnabled(true);
        binding.webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        binding.webView.getSettings().setAppCacheEnabled(true);
        binding.webView.loadUrl(url);
    }

    public void detailFindPeople(SelfWorkerPublishEntity data) {
        String userNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        if (!TextUtils.isEmpty(userNo) && data != null && userNo.equals(data.getUserNo())) {
            binding.linDetailTool.setVisibility(View.GONE);
        } else {
            binding.linDetailTool.setVisibility(View.VISIBLE);
        }

        if (data != null) {
            isAttention = data.isFocus();
            follow();
        }
    }

    public void detailTeamTelephone(SelfWorkerPublishEntity entity){
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + entity.getContactPhone());
        intent.setData(data);
        startActivity(intent);
    }

    public void follow() {
        if (isAttention) {
            binding.ivAttention.setImageResource(R.mipmap.attention_s);
            binding.tvAttention.setTextColor(ContextCompat.getColor(this, R.color.color_EF4033));
            isAttention = false;
        } else {
            binding.ivAttention.setImageResource(R.mipmap.gz_orange_no_select);
            binding.tvAttention.setTextColor(ContextCompat.getColor(this, R.color.color_666666));
            isAttention = true;
        }
    }
}
